package com.yanqu.road.server.cmd.activity.douluodalu;

import com.yanqu.road.pb.activity.douluodalu.DouLuoDaLuProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.douluodalu.DouLuoDaLuMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = Protocol.S_DOU_LUO_DA_LU_TEAM_MEMBER_MANAGE,desc = "队伍管理")
public class DouLuoDaLuTeamMemberManageCmd extends AbstractCommand {
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        if ((ret = DouLuoDaLuMgr.canEntryActivity(player)) != 0) return;

        DouLuoDaLuProto.DouLuoDaLuTeamMemberManageReqMsg reqMsg = DouLuoDaLuProto.DouLuoDaLuTeamMemberManageReqMsg.parseFrom(packet.getBytes());

        //v4.3 活动期间可审批,但不可踢人
        ret = DouLuoDaLuMgr.activityInCreateTeamPeriod();
        if(reqMsg.getStatus() == 2){
            //踢人，必须在组队期
            if(ret == GameErrorCode.E_DOU_LUO_DA_LU_ACTIVITY_NO_IN_CREATE_TEAM_TIME){
                return;
            }
        }
        if(ret == GameErrorCode.E_DOU_LUO_DA_LU_ACTIVITY_NO_IN_CREATE_TEAM_TIME){
            //其他组队期允许
            ret = 0;
        }else {
            if(ret != 0) {
                return;
            }
        }

        if(reqMsg.getTargetUserIdList().size() == 0){
            ret = GameErrorCode.E_REQUEST_ARGUMENT_ERROR;
            return;
        }
        DouLuoDaLuProto.DouLuoDaLuTeamMemberManageCrossReqMsg.Builder crossReq = DouLuoDaLuProto.DouLuoDaLuTeamMemberManageCrossReqMsg.newBuilder();
        crossReq.setActivityId(DouLuoDaLuMgr.getActivityInfo().getActivityId());
        crossReq.setStatus(reqMsg.getStatus());
        crossReq.addAllTargetUserId(reqMsg.getTargetUserIdList());
        player.sendUnionActivityGroupPacket(Protocol.C_DOU_LUO_DA_LU_TEAM_MEMBER_MANAGE, crossReq, crossReq.getActivityId());
    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            DouLuoDaLuProto.DouLuoDaLuTeamMemberManageRespMsg.Builder respMsg = DouLuoDaLuProto.DouLuoDaLuTeamMemberManageRespMsg.newBuilder();
            respMsg.setRet(ret);
            player.sendPacket(Protocol.U_DOU_LUO_DA_LU_TEAM_MEMBER_MANAGE, respMsg);
        }
    }
}
